#pragma once
#include <compl.h>
#include <libapfsds.h>

typedef struct stacknode
{
	struct
	{
		struct stacknode *next;
		void *val;
	} ALIGN(HDEF_CACHE_LINE_BYTES);
} stacknode_t;

typedef struct stack
{
	struct
	{

		stacknode_t *head;
		word_t  count;
	} ALIGN(HDEF_CACHE_LINE_BYTES);
} sstack_t;

static inline void *stack_init(sstack_t *s)
{
	s->head = NULL;
	s->count = 0;
	return s;
}

static inline void stack_push(sstack_t *s, stacknode_t *node)
{
	node->next = atm_ld(&s->head);
	s->head = node;
}

static inline stacknode_t *stack_pop(sstack_t *s)
{
	stacknode_t *ret = NULL;
	if (s->head)
	{
		ret = atm_ld(&s->head);
		s->head = atm_ld(&ret->next);
	}
	return ret;
}
word_t stack_push_cb(APFSDS_ARGLIST)
{
	PARAM_USED(arg2, arg3);
	if ((arg0 != 0) && (arg1 != 0))
		stack_push((sstack_t *)arg0, (stacknode_t *)arg1);

	return (++((sstack_t *)arg0)->count);
}

word_t stack_pop_cb(APFSDS_ARGLIST)
{
	PARAM_USED(arg1, arg2, arg3);
	return (word_t)stack_pop((sstack_t *)arg0);
}